<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 5.1.&nbsp; Calling Functions</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-5.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-5-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-5-SECT-1"></a>
<h3 id="title-IDAW2AXG" class="docSection1Title">5.1. Calling Functions</h3>
<a name="IDX-CHP-5-0171"></a> 

<p class="docText">Functions that are built into PHP can be called from any PHP script. When you call functions, you are executing the code inside them, except the code is reusable and more maintainable. One built-in function, shown in <a class="docLink" href="#learnphpmysql-CHP-5-EX-2">Example 5-2</a>, is <tt>phpinfo</tt>. It returns configuration and technical information about your PHP installation.</p>
<a name="learnphpmysql-CHP-5-EX-2"></a><h5 id="title-IDAK3AXG" class="docExampleTitle">Example 5-2. Displaying information about the PHP environment</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
    phpinfo();
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">The function helps you diagnose common problems and issues. You may find that this is one of the most helpful places to look when checking to see whether you meet the requirements of a PHP script. <a class="docLink" href="#learnphpmysql-CHP-5-FIG-2">Figure 5-2</a> shows only part of the information contained on this page. If a function call doesn't work, this page helps diagnose whether PHP is compiled with the necessary modules.</P>
<a name="learnphpmysql-CHP-5-FIG-2"></a><p><center>
<h5 class="docFigureTitle">Figure 5-2. Information about PHP displayed in the browser</H5>
<img border="0" alt="" id="195131084202" width="549" height="471" SRC="images/learnphpmysql_0502.jpg">
</center></P><br>
<p class="docText">To call a function, write the name of the function followed by an opening parenthesis <tt>(</tt>, the parameters, and then a closing parenthesis <tt>)</tt>, followed by a semicolon (<tt>;</tt>). It would look like this: <tt><I>function_name</i></tt><tt>(</tt><tt><i>parameters</i></tt><tt>);</tt>. Function names aren't case sensitive, so calling<a name="IDX-CHP-5-0172"></a> 
 <tt>phpinfo</tt> is the same as calling <tt>PhpInfo</tt>. As shown in <a class="docLink" href="#learnphpmysql-CHP-5-EX-3">Example 5-3</a>, this is what calling a function looks like: <tt>md5($mystring);</tt>.</P>
<p class="docText">Most functions have return values that you'll either use in a comparison or store in a variable. A great place to start is the <tt>md5</tt> function. <tt>md5</tt> is a one-way hash function used to verify the integrity of a string, similar to a checksum. <tt>md5</tt> converts a message into a fixed string of digits, called a message digest. You can then perform a hashcheck, comparing the calculated message digest against a message digest decrypted with a public key to verify that the message was not tampered with. <a class="docLink" href="#learnphpmysql-CHP-5-EX-3">Example 5-3</a> creates a 128-bit long <tt>md5</tt> signature of the string <tt>"mystring"</tt>.</p>
<a name="learnphpmysql-CHP-5-EX-3"></a><h5 id="title-IDAX5AXG" class="docExampleTitle">Example 5-3. Creating an md5 signature</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;?php
    $mystring = "mystring";
    $signature = md5($mystring);
    echo $signature;
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-5-EX-3">Example 5-3</a> displays:</P>
<pre>
169319501261c644a58610f967e8f9d0
</pre><BR>

<p class="docText">The return value, which is discussed in detail in this chapter, is assigned to the variable <tt>$signature</tt>, which then displays the output.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><td valign="top">
<p class="docText">The optional <tt>raw_output</tt> parameter was added in PHP 5.0 and defaults to <tt>FALSE</tt> for the <tt>md5</tt> function. There is no interpretation for <tt>raw_output</tt>.</P>
</TD></TR></table></td></tr></table></p><br>
<p class="docText">A common use for <tt>md5</tt> is to verify that a file didn't become corrupt while it was transferring. The file and its md5 signature are compared after they're received. If they match, you know that it's very unlikely that the file's contents were corrupted during transfer. If they're different, you know that the file is corrupt.</p>
<p class="docText">This example demonstrates how you can perform a complex process using a function without having to worry about how that process actually does it. This is the real power of functions.</p>

</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-5.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-5-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
